package com.study.security.core.validate.code;

import com.study.security.core.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.Random;

@Component("smsCodeGenerator")
public class SmsCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public ValidateCode generate(ServletWebRequest request) {

        Random random = new Random();
        String code = "";
        for (int i = 0; i < securityProperties.getCode().getSms().getLength(); i++) {
            String rand = String.valueOf(random.nextInt(10));
            code += rand;
        }
        return new ValidateCode(code, securityProperties.getCode().getSms().getExpireIn());
    }
}
